package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.OverflowListDao;
import com.atguigu.jxc.dao.OverflowListGoodsDao;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.OverflowListGoodsService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fenge
 */
@Service
public class OverflowListGoodsServiceImpl implements OverflowListGoodsService {

    @Autowired
    private OverflowListDao overflowListDao;

    @Autowired
    private OverflowListGoodsDao overflowListGoodsDao;

    @Override
    public void save(OverflowList overflowList, String overflowListGoodsStr, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        Integer userId = user.getUserId();

        overflowList.setUserId(userId);
        overflowListDao.save(overflowList);
        Gson gson = new Gson();
        List<OverflowListGoods> overflowListGoods = gson.fromJson(overflowListGoodsStr,new TypeToken<List<OverflowListGoods>>(){}.getType());
        overflowListGoods.forEach(overflowListGood-> {
            overflowListGood.setOverflowListId(overflowList.getOverflowListId());
            overflowListGoodsDao.save(overflowListGood);
        });
    }

    @Override
    public Map<String, Object> list(String sTime, String eTime) {
        List<OverflowList> damageLists = overflowListDao.list(sTime,eTime);
        Map<String, Object> map = new HashMap<>();
        map.put("rows",damageLists);
        return map;
    }

    @Override
    public Map<String, Object> goodsList(Integer overflowListId) {
        List<OverflowListGoods> overflowListGoods = overflowListGoodsDao.goodsList(overflowListId);
        Map<String, Object> map = new HashMap<>();
        map.put("rows",overflowListGoods);
        return map;
    }
}
/*
[{"goodsId":44,

"goodsCode":"0031",
"goodsName":"234",
"goodsModel":"234",
"goodsUnit":"元",
"lastPurchasingPrice":0,
"price":"100",
"goodsNum":"100",
"total":10000
"goodsTypeId":11,
},
{"goodsId":43,
"goodsTypeId":9,
"goodsCode":"9d9c6970-bbcb-468a-bc43-2aaa16e0f874",
"goodsName":"456",
"goodsModel":"456",
"goodsUnit":"瓶",
"lastPurchasingPrice":0,
"price":"100",
"goodsNum":"100",
"total":10000}]
 */
